<?php

declare(strict_types=1);

namespace app\index\controller;

use app\model\Post;
use app\model\Topic as ModelTopic;
use think\facade\View;
use think\Request;

class Topic extends Common
{
  /**
   * 显示资源列表
   *
   * @return \think\Response
   */
  public function index()
  {
    //

    $model_list = ModelTopic::where('status', 0)
      ->order('sort desc');

    $type = $this->request->param('type', 0);
    $sort_by = $this->request->param('sort_by', 'create_time');

    if (!empty($type)) {
      $model_list->where('type', $type);
    }

    $model_list->order($sort_by, 'desc');

    $list = $model_list->paginate();

    View::assign('list', $list);

    return View::fetch();
  }

  public function edit($id)
  {

    $model_topic  = ModelTopic::find($id);

    View::assign('topic', $model_topic);

    return View::fetch();
  }

  public function update($id)
  {
    $post_data = $this->request->post();

    $model_topic = ModelTopic::find($id);

    $model_topic->save($post_data);

    return json_message();
  }

  public function statusManage()
  {
    $list_topic = ModelTopic::where('status', $this->request->param('status', 0))
      ->order('id desc')
      ->paginate();


    View::assign('list_topic', $list_topic);
    return View::fetch();
  }
}
